﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace ERPClient
{
    class CursorCreator
    {
        public struct IconInfo
        {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
        [DllImport("user32.dll")]
        public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

        public static Cursor temp1 = null;
        public static Cursor temp2 = null;

        public static void Init()
        {
            if (temp1 == null)
            {
                Bitmap t1 = new Bitmap("..\\..\\Resources\\addgroupfooter_32x32.png");
                temp1 = CreateCursor(t1, new Point(0, 0));

                Bitmap t2 = new Bitmap("..\\..\\Resources\\addgroupheader_32x32.png");
                temp2 = CreateCursor(t2, new Point(0, 0));
            }
        }

        public static Cursor Cursor1() { return temp1; }
        public static Cursor Cursor2() { return temp2; }

        /// <summary>
        /// Create a cursor from a bitmap without resizing and with the specified
        /// hot spot
        /// </summary>
        public static Cursor CreateCursor(Bitmap bmp, Point hotspot)
        {
            IntPtr ptr = bmp.GetHicon();
            IconInfo tmp = new IconInfo();
            GetIconInfo(ptr, ref tmp);
            tmp.xHotspot = hotspot.X;
            tmp.yHotspot = hotspot.Y;
            tmp.fIcon = false;
            ptr = CreateIconIndirect(ref tmp);
            return new Cursor(ptr);
        }


        /// <summary>
        /// Create a 32x32 cursor from a bitmap, with the hot spot in the middle
        /// </summary>
        //public static Cursor CreateCursor(Bitmap bmp)
        //{
        //    int xHotSpot = 16;
        //    int yHotSpot = 16;

        //    IntPtr ptr = ((Bitmap)ResizeImage(bmp, 32, 32)).GetHicon();
        //    IconInfo tmp = new IconInfo();
        //    GetIconInfo(ptr, ref tmp);
        //    tmp.xHotspot = xHotSpot;
        //    tmp.yHotspot = yHotSpot;
        //    tmp.fIcon = false;
        //    ptr = CreateIconIndirect(ref tmp);
        //    return new Cursor(ptr);
        //}
    }
}
